/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.config.Global;
import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.DateUtils;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.utils.excel.ExportExcel;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.ProFiling;
import com.zx.pm.modules.zx.service.LzdService;
import com.zx.pm.modules.zx.service.ProFilingService;
import com.zx.pm.modules.zx.service.TenderSectionService;

/**
 * 项目归档Controller
 * @author forest
 * @version 2019-02-19
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/proFiling")
public class ProFilingController extends BaseController {

	@Autowired
	private ProFilingService proFilingService;
	
	@Autowired
	private LzdService lzdService;
	
	@Autowired
	private TenderSectionService tenderSectionService;
	
	@ModelAttribute
	public ProFiling get(@RequestParam(required=false) String lzdId) {
		ProFiling entity = null;
		if (StringUtils.isNotBlank(lzdId)){
			entity = proFilingService.get(lzdId);
		}
		if (entity == null){
			entity = new ProFiling();
		}
		// 流转单ID
		entity.setLzd(lzdService.get(lzdId));
		return entity;
	}
	
	@RequiresPermissions("zx:proFiling:view")
	@RequestMapping(value = { "list", "" })
	public String list(ProFiling proFiling, HttpServletRequest request, HttpServletResponse response, Model model) {
		Lzd lzd = proFiling.getLzd();
		if (lzd == null) {
			lzd = new Lzd();
		}
		proFiling.setLzd(lzd);
		
		if(proFiling.getContractsDate() != null){
			proFiling.setSerchContractsDate(DateUtils.formatDate(proFiling.getContractsDate(), "yyyy-MM"));
		}
		Page<ProFiling> page = proFilingService.findPage(new Page<ProFiling>(request, response), proFiling);
		model.addAttribute("lzd", lzd);
		model.addAttribute("page", page);
		return "modules/zx/proFilingList";
	}

	@RequiresPermissions("zx:proFiling:view")
	@RequestMapping(value = "form")
	public String form(ProFiling proFiling, Model model) {

		proFiling.setTs(tenderSectionService.get(proFiling.getSectionId()));
		// 获取项目保证金
		if(proFiling.getTs() != null) {
			proFiling.setDepositMoney(lzdService.getDepositMoney(proFiling.getLzdId(), proFiling.getSectionId()));
		}
		model.addAttribute("proFiling", proFiling);
		return "modules/zx/proFilingForm";
	}

	@RequiresPermissions("zx:proFiling:edit")
	@RequestMapping(value = "save")
	public String save(ProFiling proFiling, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proFiling)){
			return form(proFiling, model);
		}
		proFilingService.save(proFiling);
		addMessage(redirectAttributes, "保存项目归档成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proFiling/?repage";
	}
	
	@RequiresPermissions("zx:proFiling:edit")
	@RequestMapping(value = "delete")
	public String delete(ProFiling proFiling, RedirectAttributes redirectAttributes) {
		proFilingService.delete(proFiling);
		addMessage(redirectAttributes, "删除项目归档成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proFiling/?repage";
	}

	
	/**
	 * 导出项目信息数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(ProFiling proFiling, HttpServletRequest request, HttpServletResponse response, 
    		RedirectAttributes redirectAttributes) {
		try {
            String fileName = "项目信息数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<ProFiling> list = proFilingService.findList(proFiling);
    		new ExportExcel("项目信息", ProFiling.class).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出数据失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/zx/proFiling/list?repage";
    }
}